package com.smart.campus.web.controller.school;

import com.smart.campus.common.annotation.Log;
import com.smart.campus.common.core.controller.BaseController;
import com.smart.campus.common.core.domain.AjaxResult;
import com.smart.campus.common.core.page.TableDataInfo;
import com.smart.campus.common.enums.BusinessType;
import com.smart.campus.common.utils.poi.ExcelUtil;
import com.smart.campus.school.domain.ClassInfo;
import com.smart.campus.school.domain.Grade;
import com.smart.campus.school.domain.Wisdom;
import com.smart.campus.school.domain.request.WisdomList;
import com.smart.campus.school.service.IClassInfoService;
import com.smart.campus.school.service.IWisdomService;
import com.smart.campus.web.utils.SchoolUtil;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * 班级信息Controller
 * 
 * @author Zhihe.Li
 * @date 2020-05-13
 */
@Controller("classInfo")
@RequestMapping("/school/classInfo")
public class ClassInfoController extends BaseController
{
    private String prefix = "school/classInfo";

    @Autowired
    private IClassInfoService classInfoService;

    @Autowired
    private IWisdomService iWisdomService;

    @RequiresPermissions("school:classInfo:view")
    @GetMapping()
    public String classInfo()
    {
        return prefix + "/classInfo";
    }

    /**
     * 查询班级信息列表
     */
    @RequiresPermissions("school:classInfo:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(ClassInfo classInfo)
    {
        startPage();
        classInfo.setDeptId(SchoolUtil.getSchoolId());
        List<ClassInfo> list = classInfoService.selectClassInfoList(classInfo);
        return getDataTable(list);
    }

    /**
     * 导出班级信息列表
     */
    @RequiresPermissions("school:classInfo:export")
    @Log(title = "班级信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(ClassInfo classInfo)
    {
        classInfo.setDeptId(SchoolUtil.getSchoolId());
        List<ClassInfo> list = classInfoService.selectClassInfoList(classInfo);
        ExcelUtil<ClassInfo> util = new ExcelUtil<ClassInfo>(ClassInfo.class);
        return util.exportExcel(list, "classInfo");
    }

    /**
     * 新增班级信息
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存班级信息
     */
    @RequiresPermissions("school:classInfo:add")
    @Log(title = "班级信息", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(ClassInfo classInfo)
    {
        classInfo.setDeptId(SchoolUtil.getSchoolId());
        classInfoService.insertClassInfo(classInfo);
        return AjaxResult.success(classInfo.getId());
    }

    /**
     * 修改班级信息
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        ClassInfo classInfo = classInfoService.selectClassInfoById(id);
        mmap.put("classInfo", classInfo);
        return prefix + "/edit";
    }

    /**
     * 修改保存班级信息
     */
    @RequiresPermissions("school:classInfo:edit")
    @Log(title = "班级信息", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(ClassInfo classInfo)
    {
        return toAjax(classInfoService.updateClassInfo(classInfo));
    }

    /**
     * 删除班级信息
     */
    @RequiresPermissions("school:classInfo:remove")
    @Log(title = "班级信息", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(classInfoService.deleteClassInfoByIds(ids));
    }


    /**
     * 获取班级列表
     */
    @PostMapping("/getClassList")
    @ResponseBody
    public List<ClassInfo> getClassList(Long gradeId) {

        ClassInfo classInfo = new ClassInfo();
        classInfo.setStatus(1);
        classInfo.setDeptId(SchoolUtil.getSchoolId());
        classInfo.setGradeId(gradeId);
        return classInfoService.selectClassInfoList(classInfo);
    }

    /**
     * 保存班级名言警句
     */
    @PostMapping("/saveWisdom")
    @ResponseBody
    public AjaxResult saveWisdom(WisdomList wisdomList) {


        if (wisdomList != null){

            iWisdomService.deleteWisdomByClassId(wisdomList.getClassId());

            List<Wisdom> wisdoms = wisdomList.getWisdomList();
            for (Wisdom wisdom : wisdoms) {
                wisdom.setCreateTime(new Date());
                iWisdomService.insertWisdom(wisdom);
            }

        }

        return AjaxResult.success();

    }
}
